package column


import (
	"github.com/treelab/updatecolumn/models"
)

  func instanceType() int32 {
    return models.Number
  }

  deleteColumnEdges(command: UpdateColumn_v2, o: CommonOptions) {
    const { column } = command;
    o.delQuads.push(...this.dgraphHelperService.getRemoveEdgeQuads(column.uid!, ['precision', 'defaultNumber']));
    return this;
  }

  public hasNothingToUpdate(command: UpdateColumn_v2) {
    const { column, columnConfig } = command;
    const toColumnConfig = columnConfig as NumberColumnConfig;
    return (
      column.columnType === this.instanceType &&
      column.precision === toColumnConfig.precision &&
      column.defaultNumber === toColumnConfig.defaultNumber
    );
  }

  async updateColumn(
    command: UpdateColumn_v2,
    o: CommonOptions,
    helper: { rows?: Row[]; columnCopy?: Column; referenceColumns?: Column[]; userGroup?: any },
  ) {
    await super.updateColumn(command, o, helper);

    const { column, columnConfig } = command;
    const { defaultNumber, precision } = columnConfig as NumberColumnConfig;

    column.precision = precision;
    if (defaultNumber !== undefined) {
      column.defaultNumber = defaultNumber;
    }

    return this;
  }

  async convertColumn(
    command: UpdateColumn_v2,
    o: CommonOptions,
    helper: { rows?: Row[]; columnCopy?: Column; referenceColumns?: Column[]; userGroup?: any },
  ) {
    const { column, columnConfig } = command;
    const currencyColumnConfig = command.columnConfig as CurrencyColumnConfig;
    if (column.columnType === ColumnType.NUMBER) {
      if (
        column.precision === currencyColumnConfig.precision &&
        column.defaultNumber === currencyColumnConfig.defaultNumber &&
        column.columnType === columnConfig.type
      ) {
        return this;
      }
      if (column.defaultNumber && currencyColumnConfig.defaultNumber === undefined) {
        // Remove existing default number if the new config is removing the default number
        o.delQuads.push(...this.dgraphHelperService.getRemoveEdgeQuads(column.uid!, 'defaultNumber'));
      }
    }

    await this.defaultConvert({ command }, o, helper);
    return this;
  }

